import json
import time

from flask import Blueprint, request
from flask_restful import Resource, Api

import DcUrls

listData = Blueprint('ListData', __name__)
api = Api(listData)


class ListData(Resource):
    def get(self):
        pageSize = int(request.args.get("pageSize", "0"))
        pageNumber = int(request.args.get("pageNumber", "10"))
        return list(pageSize, pageNumber, "首页")

    def post(self):
        reqJson = json.loads(request.data)
        print(reqJson)
        pageSize = int(reqJson["pageSize"])
        pageNumber = int(reqJson["pageNumber"])
        return list(pageSize, pageNumber, "首页")


class ListDataNew(Resource):
    def get(self):
        pageSize = int(request.args.get("pageSize", "0"))
        pageNumber = int(request.args.get("pageNumber", "10"))
        return list(pageSize, pageNumber, "新闻")

    def post(self):
        reqJson = json.loads(request.data)
        print(reqJson)
        pageSize = int(reqJson["pageSize"])
        pageNumber = int(reqJson["pageNumber"])
        return list(pageSize, pageNumber, "新闻")


class ListDataAddres(Resource):
    def get(self):
        pageSize = int(request.args.get("pageSize", "0"))
        pageNumber = int(request.args.get("pageNumber", "10"))
        return list(pageSize, pageNumber, "通讯录")

    def post(self):
        reqJson = json.loads(request.data)
        print(reqJson)
        pageSize = int(reqJson["pageSize"])
        pageNumber = int(reqJson["pageNumber"])
        return list(pageSize, pageNumber, "通讯录")


api.add_resource(ListData, '/getListData')
api.add_resource(ListDataNew, '/getListDataNew')
api.add_resource(ListDataAddres, '/getListDataAddress')


def list(pageSize, pageNumber, title):
    data = {}
    data["pageNum"] = pageNumber
    data["pageSize"] = pageSize
    data["startRow"] = 1
    data["endRow"] = 2
    data["total"] = 100 * pageSize
    data["pages"] = 100

    lists = []
    for i in range(pageSize * pageNumber, pageSize * pageNumber + pageSize):
        lists.append(getListContent(i, title))

    data["list"] = lists
    result = setResult(data)
    return result


def getListContent(i, title):
    d = {}
    d["key"] = "key" + str(i)
    d["title"] = title + str(i)
    if i % 2 == 0:
        d["titleImageUrl"] = DcUrls.Url + "/static/images/title.png"
    d["date"] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    d["content"] = "实现好、维护好、发展好最广大人民根本利益是我们党一切工作的出发点和落脚点。" \
                   "党的十八大以来，以习近平同志为核心的党中央坚持以人民为中心的发展思想，着眼于在发展中补齐民生短板、" \
                   "兜牢民生底线，聚焦基层的困难事、群众的烦心事，将一大批惠民举措落地落实，" \
                   "实现基本公共服务均等化水平显著提升，增强人民群众的获得感、幸福感、安全感。"
    if i % 2 == 0:
        d["contentImageUrl"] = DcUrls.Url + "/static/images/22.jpg"
    # d["maxPage"] = 2

    return d


def setResult(data):
    """
    通用返回数据格式
    :param data: 详细数据
    :return: result
    """
    result = {"code": 200, "message": "获取列表数据成功", "data": data}
    return result
